const db = require('../utils/db')
// 查询视频详细部分sql语句
const comm = `
  select v.id, title, intro, videoUrl, coverUrl, v.status,
    v.userId, username, nickname, avatarUrl,
    (select count(*) from videolikes vl where vl.videoId=v.id) as likeCount,
    (select count(*) from videocollects vc where vc.videoId=v.id) as collectCount,
    (select count(*) from comments c where c.videoId=v.id) as commentCount`

// 分页获取视频
exports.getPageVideos = (req, res)=>{
  const size = req.query.pagesize? req.query.pagesize: 3
  const offset = req.query.pagenum*size 
  const sql = `${comm}
    from videos v,users u
    where v.userId=u.id and v.status=0
    order by v.createAt desc, likeCount desc
    limit ${offset},${size}`
  db.query(sql, (err, results)=>{
    if(err) return res.er(err)
    res.send({code:0, msg:'获取视频成功', videos: results})
  })
}

// 上传视频
exports.uploadVideo = (req, res)=>{
  const sql = 'insert into videos set ?'
  const values = {
    ...req.body,
    videoUrl: req.files[0].path,
    coverUrl: req.files[1].path,
    userId: req.user.id,
    createAt: Date.now()
  }
  db.query(sql, values, (err, result)=>{
    if(err) return res.er(err)
    if(result.affectedRows != 1) return res.er('上传视频失败')
    res.send({code:0, msg:'上传视频成功'})
  })
}

// 点赞视频
exports.likeVideo = (req, res)=>{
  const sql = `insert into videolikes set ?`
  const createAt = Date.now()
  const values = {
    videoId: req.params.id,
    userId: req.user.id,
    createAt
  }
  db.query(sql, values, (err, results)=>{
    if(err) return res.er(err)
    res.send({
      code:0, 
      msg:'点赞视频成功',
      dt: [{videoId: req.params.id}]
    })
  })
}

// 取消点赞视频
exports.unlikeVideo = (req, res)=>{
  const sql = `delete from videolikes where videoId=${req.params.id}`
  db.query(sql, (err, results)=>{
    if(err) return res.er(err)
    res.send({
      code:0, 
      msg:'取消点赞视频成功',
      // videoId: req.params.id
    })
  })
}

// 获取用户点赞过的视频
exports.getLikeVideos = (req, res)=>{
  const sql = `select videoId from videolikes where userId=${req.user.id}`
  db.query(sql, (err, results)=>{
    if(err) return res.er(err)
    res.send({code:0, msg:'获取用户点赞视频成功', dt:results})
  })
}

// 收藏视频
exports.collectVideo = (req, res)=>{
  const sql = `insert into videocollects set ?`
  const createAt = Date.now()
  const values = {
    videoId: req.params.id,
    userId: req.user.id,
    createAt
  }
  db.query(sql, values, (err, results)=>{
    if(err) return res.er(err)
    res.send({
      code:0, 
      msg:'收藏视频成功',
      dt: [{videoId: req.params.id}]
    })
  })
}

// 取消收藏视频
exports.uncollectVideo = (req, res)=>{
  const sql = `delete from videocollects where videoId=${req.params.id}`
  db.query(sql, (err, results)=>{
    if(err) return res.er(err)
    res.send({
      code:0, 
      msg:'取消收藏视频成功',
      // videoId: req.params.id
    })
  })
}

//  获取用户点收藏过的视频
exports.getCollectVideos = (req, res)=>{
  const sql = `select videoId from videocollects where userId=${req.user.id}`
  db.query(sql, (err, results)=>{
    if(err) return res.er(err)
    res.send({code:0, msg:'获取用户点赞视频成功', dt:results})
  })
}

// 根据关键字搜索视频
exports.selectVideosByKw = (req, res)=>{
  const sql = `${comm}
    from videos v,users u
    where v.userId=u.id and v.status=0 and v.title like '%${req.params.kw}%'
    order by v.createAt desc, likeCount desc`
  db.query(sql, (err, results)=>{
    if(err) return res.er(err)
    res.send({code:0, msg:'搜索视频成功', dt: results})
  })
}

// 获取用户发布、点赞、收藏的视频
exports.selectVideosOfUser = (req, res)=>{
  // 用户发布的视频 
  const sql1 = `${comm}
  from videos v ,users u
  where v.userId=${req.user.id} and u.id=${req.user.id}
  order by v.createAt desc, likeCount desc`
  // 用户点赞的视频
  const sql2 = `${comm}
    from videos v ,users u, videolikes vl
    where v.userId=u.id and vl.videoId=v.id and vl.userId=${req.user.id}
    order by v.createAt desc, likeCount desc`
  // 用户收藏的视频
  const sql3 = `${comm}
    from videos v ,users u, videocollects vc
    where v.userId=u.id and vc.videoId=v.id and vc.userId=${req.user.id}
    order by v.createAt desc, likeCount desc`
  db.query(sql1, (err, results1)=>{
    if(err) return res.er(err)
    db.query(sql2, (err,results2)=>{
      if(err) return res.er(err)
      db.query(sql3, (err,results3)=>{
        if(err) return res.er(err)
        res.send({code:0, msg:'获取用户发布、点赞、收藏的视频成功', 
          dt: {
            publishes: results1,
            likes: results2,
            collects: results3
          }
        })
      })
    })
  })
}

// 获取作家主页数据
exports.getAuthorData = (req, res)=>{
  console.log(req.params);
  let sql = `select id,username,nickname,avatarUrl,backgroundUrl,signature
    from users where id=${req.params.id}`
  db.query(sql, (err, results)=>{
    if(err) return res.er(err)
    sql = `${comm}
      from videos v ,users u
      where v.userId=${req.params.id} and u.id=${req.params.id}
      order by v.createAt desc, likeCount desc`
    db.query(sql, (err, results2)=>{
      if(err) return res.er(err)
      res.send({
        code:0, 
        msg:'获取作家数据成功', 
        dt:{
          author: results[0],
          works: results2 
        }
      })
    })
  })
}